package com.blb.zy_community.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zy_community.entity.ResponseResult;
import com.blb.zy_community.entity.SysPost;
import com.blb.zy_community.entity.ZyCommunityInteraction;
import com.blb.zy_community.entity.ZyOwner;
import com.blb.zy_community.mapper.ZyOwnerMapper;
import com.blb.zy_community.service.IZyCommunityInteractionService;
import com.blb.zy_community.service.impl.ZyOwnerServiceImpl;
import com.blb.zy_community.utils.JwtUtils;
import com.blb.zy_community.utils.RsaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * 社区互动表 前端控制器
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@RestController
@CrossOrigin
public class ZyCommunityInteractionController {
    @Autowired
    private IZyCommunityInteractionService communityInteractionService;
    @Autowired
    private ZyOwnerServiceImpl zyOwnerService;
    @Autowired
    private ZyOwnerMapper zyOwnerMapper;
    @RequestMapping("/interaction-page")
    public ResponseResult<Page<ZyCommunityInteraction>> getPostPage(@RequestParam(required = false,defaultValue = "1") Long current,
                                                                    @RequestParam(required = false,defaultValue = "5")Long size,
                                                                    @RequestParam(required = false)String ownerNickname,
                                                                    @RequestParam(required = false)String ownerRealName,
                                                                    @RequestParam(required = false)String ownerPhoneNumber
    ){
        Page<ZyCommunityInteraction> page = communityInteractionService.getInteractionPage(current, size, ownerNickname, ownerRealName,ownerPhoneNumber);
        return ResponseResult.ok(page);
    }


    @DeleteMapping("/interaction/{id}")
    public ResponseResult<String> deleteInteraction(@PathVariable Long id){
        communityInteractionService.removeById(id);
        return ResponseResult.ok("ok");
    }
    /**
     * 批量删除
     * @param
     * @return
     */
    @DeleteMapping("/interactions/{interactionIds}")
    public ResponseResult<String> deleteInteractions(@PathVariable Long[] interactionIds){
        communityInteractionService.deleteInteraction(interactionIds);
        return ResponseResult.ok(interactionIds);
    }

    @PostMapping("/interaction")
    public ResponseResult<ZyCommunityInteraction> saveContent(@RequestBody  ZyCommunityInteraction zyCommunityInteraction,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String createUser = JwtUtils.getUsernameFromToken(token, RsaUtils.publicKey);
        ZyOwner owner = zyOwnerMapper.selectOne(new QueryWrapper<ZyOwner>().eq("owner_real_name", createUser));
        zyCommunityInteraction.setCommunityId(1338423709557272577L);
        zyCommunityInteraction.setCreateBy(String.valueOf(owner.getOwnerId()));
        zyCommunityInteraction.setCreateTime(LocalDateTime.now());
        zyCommunityInteraction.setUserId(owner.getOwnerId());
        communityInteractionService.save(zyCommunityInteraction);
        return ResponseResult.ok(zyCommunityInteraction);
    }



}
